﻿using Rabbit.Common;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rabbit.Consumer.Dead
{
    internal class ReceiveDead
    {

        /// <summary>
        /// 接收消息
        /// </summary>
        public static void ReceiveDeadMsg()
        {
           
            //创建连接
            var conn = RabbitMQHelper.GetConnect();
            //创建信道
            var channel = conn.CreateModel();
           

            var consumer = new EventingBasicConsumer(channel);//消费者
            consumer.Received += (m, e) =>
            {
                //业务处理

                //二进制流转字符串 (Encoding.UTF8.GetString 方法是用于将UTF-8编码的字节数组转换为一个字符串的方法。)
                string msg = Encoding.UTF8.GetString(e.Body.ToArray());
                //Thread.Sleep(500);
                //(3)消息被拒绝。  消费端拒绝已传递的消息。
                channel.BasicReject(e.DeliveryTag,requeue:true);//requeue:false 死信，true返回原队列

                Console.WriteLine("已经消费了消息:" + msg);

               

            };

            //慎用自动签收
            channel.BasicConsume(queue: "NormalQue", autoAck: false, consumer);//自动签收 (autoAck:true)

        }

    }
}
